package cn.lili.controller.auth;

import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.auth.service.ApplicationService;
import cn.lili.modules.member.client.FootPrintClient;
import cn.lili.modules.member.entity.dos.FootPrint;
import cn.lili.modules.member.entity.vo.ApplicationVO;
import cn.lili.modules.search.client.CustomWordsClient;
import cn.lili.modules.search.entity.vo.CustomWordsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * ApplicationCommonApi
 *
 * @author Chopper
 * @version v1.0
 * 2022-08-29 14:44
 */
@RestController
@Api(tags = "应用认证接口")
@RequestMapping("/auth/application/oauth")
@RequiredArgsConstructor
public class ApplicationCommonApi {

    private final ApplicationService applicationService;

    private final CustomWordsClient customWordsClient;

    private final FootPrintClient footPrintClient;


    @ApiOperation(value = "oauth2 step1 获取应用")
    @ApiImplicitParam(name = "appid", value = "应用ID", required = true)
    @GetMapping
    public ResultMessage<Object> oauth2(@NotNull(message = "appid cannot be empty") @RequestParam String appid) {
        ApplicationVO applicationVO = applicationService.getByAppid(appid);
        CustomWordsVO customWordsVO = new CustomWordsVO();
        customWordsVO.setName("测试");
        customWordsClient.addCustomWords(customWordsVO);
        FootPrint footPrint = new FootPrint();
        footPrintClient.saveFootprint(footPrint);
        return ResultUtil.data(applicationVO);
    }

    @ApiOperation(value = "oauth2 step2 获取code")
    @ApiImplicitParam(name = "appid", value = "应用ID", required = true)
    @GetMapping("/generalCode")
    public ResultMessage<Object> generalCode(@NotNull(message = "appid cannot be empty") @RequestParam String appid) {
        return ResultUtil.data(applicationService.generalCode(appid));
    }


    @ApiOperation(value = "oauth2 step3 生成TOKEN")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appid", value = "应用ID", required = true),
            @ApiImplicitParam(name = "code", value = "临时token", required = true)
    })
    @GetMapping("/login")
    public ResultMessage<Object> login(
            @NotNull(message = "appid cannot be empty") @RequestParam String appid,
            @NotNull(message = "code cannot be empty") @RequestParam String code
    ) {
        return ResultUtil.data(applicationService.login(appid, code));
    }



}
